// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class player extends cc.Component {

     
    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello11';

    @property
    public jumpHeight = 180.0

    @property
    jumpDuration = 0.3

    @property
    maxMoveSpeed = 400.0

    @property
    accLeft = false
    @property
    accRight = false

    @property
    xSpeed = 0

    @property
    accel = 280

     // 跳跃音效资源
     @property
     ({ 
        type: cc.AudioClip
     }) 
    jumpAudio: cc.AudioClip = null
    
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //let jumpAction = this.runJumpAction();
       // cc.tween(this.node).then(jumpAction).start()

        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;
        // 主角当前水平方向速度
        this.xSpeed = 0;

        // 初始化键盘输入监听
        //cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown1,this)
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp1,this)

         // touch input
         this.node.parent.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan, this);
         this.node.parent.on(cc.Node.EventType.TOUCH_END, this.onTouchEnded, this);
    }

    start () {

    }

    runJumpAction() {
          // 跳跃上升
          let jumpUp = cc.tween().by(this.jumpDuration, {y: this.jumpHeight}, {easing: 'sineOut'});
          // 下落
          let jumpDown = cc.tween().by(this.jumpDuration, {y: -this.jumpHeight}, {easing: 'sineIn'});
  
          // 创建一个缓动，按 jumpUp、jumpDown 的顺序执行动作
          let tween = cc.tween().sequence(jumpUp, jumpDown)
                // 添加一个回调函数，在前面的动作都结束时调用我们定义的 playJumpSound() 方法
                .call(this.playJumpSound, this);
          // 不断重复
          return cc.tween().repeatForever(tween);

    }
    playJumpSound() {
        // 调用声音引擎播放声音
        cc.audioEngine.playEffect(this.jumpAudio, false);
    }

    onKeyDown1(event) {
        // set a flag when key pressed
        console.log("---keydown---")
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = true;
                break;
            case cc.macro.KEY.d:
                this.accRight = true;
                break;
        }
    }
    onKeyUp1(event) {
        // unset a flag when key released
        console.log("---keyup---")
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft = false;
                break;
            case cc.macro.KEY.d:
                this.accRight = false;
                break;
        }
    }
    
    onTouchBegan (event) {
        var touchLoc = event.getLocation();
        if (touchLoc.x >= cc.winSize.width/2) {
            this.accLeft = false;
            this.accRight = true;
        } else {
            this.accLeft = true;
            this.accRight = false;
        }
        // don't capture the event
        return true;
    }

    onTouchEnded (event) {
        this.accLeft = false;
        this.accRight = false;
    }

    
    update (dt) {
        if(this.accLeft) {
            this.xSpeed -= this.accel * dt
        }
        if(this.accRight) {
            this.xSpeed += this.accel * dt
        }

        if(Math.abs(this.xSpeed) > this.maxMoveSpeed) {
            this.xSpeed = this.maxMoveSpeed * this.xSpeed / Math.abs(this.xSpeed)
        }

        this.node.x += this.xSpeed * dt
    }

    startMoveAt(pos) {
        this.enabled = true;
        this.xSpeed = 0;
        this.node.setPosition(pos);
        
        var jumpAction = this.runJumpAction();
        cc.tween(this.node).then(jumpAction).start()
    }

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown1,this)
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp1,this)
    }
}
